package com.android.tea.widght;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.tea.R;

public class SettingItemButton extends LinearLayout {
	private RelativeLayout mBarView;
	private LayoutInflater mInflater;
	private TextView settingitem_title;
	private ImageView settingitem_togo;
	private LinearLayout settingitem_icon;

	public SettingItemButton(Context context) {
		super(context);
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mBarView = (RelativeLayout) mInflater.inflate(R.layout.settingitem,
				null);

		addView(mBarView);
		settingitem_title = (TextView) mBarView
				.findViewById(R.id.settingitem_title);
		settingitem_togo = (ImageView) mBarView
				.findViewById(R.id.settingitem_togo);
		settingitem_icon = (LinearLayout) mBarView
				.findViewById(R.id.settingitem_icon);
	}

	public SettingItemButton(Context context, AttributeSet attr) {
		super(context);
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mBarView = (RelativeLayout) mInflater.inflate(R.layout.settingitem,
				null);
		addView(mBarView);
		settingitem_title = (TextView) mBarView
				.findViewById(R.id.settingitem_title);
		settingitem_togo = (ImageView) mBarView
				.findViewById(R.id.settingitem_togo);
		settingitem_icon = (LinearLayout) mBarView
				.findViewById(R.id.settingitem_icon);
	}

	public void setSettingIcon(int resId) {
		settingitem_togo.setImageResource(resId);
	}

	public void setTitle(String title) {
		if (settingitem_title != null && title != null) {
			settingitem_title.setText(title);
		}
	}

	public void setItemOnClickListener(OnClickListener l) {
		mBarView.setOnClickListener(l);
	}

	TextView tView;

	public void setMessage(String message) {
		settingitem_icon.removeAllViews();
		tView = new TextView(getContext());
		tView.setText(message);
		tView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
		tView.setPadding(5, 5, 5, 5);
		settingitem_icon.addView(tView);
	}

	public void setMessageColor(int color) {

		if (tView != null)
			tView.setTextColor(color);
	}

	public void setMessageBackground(int resid) {
		settingitem_icon.setBackgroundResource(resid);
	}

	public void setIcon(Bitmap icon) {
		settingitem_icon.removeAllViews();
		ImageView iView = new ImageView(getContext());
		iView.setImageBitmap(icon);
		settingitem_icon.addView(iView);
	}

	int idUserItem = 0;

	public int getIdUserItem() {
		return mBarView.getId();
	}

	public void setIdUserItem(int idUserItem) {
		mBarView.setId(idUserItem);
	}

	boolean isChecked;

	public boolean getChecked() {
		return isChecked;
	}

	public boolean setChange() {
		if (getChecked()) {
			setOff();
		} else {
			setOn();
		}
		return getChecked();

	}

	public void setOnOrOff(boolean isChecked) {
		if (isChecked) {
			setOn();
		} else {
			setOff();
		}
	}

	public void setOn() {
		isChecked = true;

		settingitem_togo.setImageResource(R.drawable.toggle_on);
	}

	public void setOff() {
		isChecked = false;

		settingitem_togo.setImageResource(R.drawable.toggle_off);
	}

}
